package com.getpebble.android.framework.developer;

import android.content.Context;
import android.database.ContentObserver;
import com.getpebble.android.common.core.async.PblAsyncTask;
import com.getpebble.android.common.core.trace.Trace;
import com.getpebble.android.common.core.util.PblContentProviderUtil;
import com.getpebble.android.common.model.PblDevice;
import com.getpebble.android.common.model.PblDeviceModel;
import com.getpebble.android.common.model.PblDeviceModel.PblDeviceRecord;
import com.getpebble.android.common.model.PblDeviceModel.Query;
import java.lang.ref.WeakReference;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

public class PblDeviceObserver extends ContentObserver {
    private final String TAG = getClass().getSimpleName();
    private WeakReference<Context> mContext;
    private PblDeviceRecord mLastConnectedDeviceRecord;
    private CopyOnWriteArrayList<ConnectedDeviceChangeListener> mListeners = new CopyOnWriteArrayList();
    private PblDeviceRecord mPblDeviceRecord;

    public interface ConnectedDeviceChangeListener {
        void notifyConnectedDeviceChanged();
    }

    private class FindConnectedAndLastConnectedDeviceAsyncTask extends PblAsyncTask {
        private FindConnectedAndLastConnectedDeviceAsyncTask() {
        }

        public boolean doInBackground() {
            PblDeviceObserver.this.setDeviceRecord(PblDeviceObserver.this.fetchDeviceRecordForQuery(Query.CONNECTION_STATUS_CONNECTED));
            PblDeviceObserver.this.setLastConnectedDeviceRecord(PblDeviceObserver.this.fetchDeviceRecordForQuery(Query.LAST_CONNECTED));
            return true;
        }

        public void onTaskSuccess() {
        }

        public void onTaskFailed() {
        }
    }

    public PblDeviceObserver(Context context) throws IllegalArgumentException {
        super(null);
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        this.mContext = new WeakReference(context);
        register(context);
        new FindConnectedAndLastConnectedDeviceAsyncTask().submit();
    }

    public void register(Context context) {
        context.getContentResolver().registerContentObserver(PblContentProviderUtil.getTableUri("devices"), true, this);
    }

    public void unregister(Context context) {
        context.getContentResolver().unregisterContentObserver(this);
    }

    public void addListener(ConnectedDeviceChangeListener listener) {
        this.mListeners.add(listener);
    }

    public void removeListener(ConnectedDeviceChangeListener listener) {
        this.mListeners.remove(listener);
    }

    public Context getContext() {
        if (this.mContext != null) {
            return (Context) this.mContext.get();
        }
        return null;
    }

    public PblDevice getDevice() {
        PblDeviceRecord deviceRecord = getPblDeviceRecord();
        if (deviceRecord != null) {
            return deviceRecord.pblDevice;
        }
        return null;
    }

    public synchronized PblDeviceRecord getLastConnectedDeviceRecord() {
        return this.mLastConnectedDeviceRecord;
    }

    public synchronized PblDeviceRecord getPblDeviceRecord() {
        return this.mPblDeviceRecord;
    }

    private synchronized void setDeviceRecord(PblDeviceRecord deviceRecord) {
        this.mPblDeviceRecord = deviceRecord;
        Iterator i$ = this.mListeners.iterator();
        while (i$.hasNext()) {
            ((ConnectedDeviceChangeListener) i$.next()).notifyConnectedDeviceChanged();
        }
    }

    private synchronized void setLastConnectedDeviceRecord(PblDeviceRecord deviceRecord) {
        this.mLastConnectedDeviceRecord = deviceRecord;
    }

    private PblDeviceRecord fetchDeviceRecordForQuery(Query query) {
        Context context = getContext();
        if (context == null) {
            Trace.warning(this.TAG, "Cannot get connected device: context is null");
            return null;
        }
        List<PblDeviceRecord> recordList = PblDeviceModel.getPblDeviceRecords(context.getContentResolver(), query);
        if (recordList.size() != 0) {
            return (PblDeviceRecord) recordList.get(0);
        }
        Trace.warning(this.TAG, "No connected devices found.");
        return null;
    }

    public void onChange(boolean selfChange) {
        super.onChange(selfChange);
        new FindConnectedAndLastConnectedDeviceAsyncTask().submit();
    }
}
